<template>
  <div  style="width:300px">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="借款人" prop="debtor">
        <el-input v-model="ruleForm.debtor"></el-input>
      </el-form-item>


      <el-form-item label="还款人" prop="payer">
        <el-input v-model="ruleForm.payer"></el-input>
      </el-form-item>


      <el-form-item label="借款日期" required>
        <el-form-item prop="begin">
          <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.begin" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-form-item>

      <el-form-item label="还款日期">
        <el-form-item prop="end">
          <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.end" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-form-item>


      <el-form-item label="金额" prop="money">
        <el-input v-model="ruleForm.money"></el-input>
      </el-form-item>

      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="ruleForm.userId"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>


    </el-form>
  </div>
</template>

<script>
  export default {
    name: "updateDebt",
    data() {
      return {
        ruleForm: {
          begin: '',
          end: '',
          debtor: '',
          payer: '',
          money: '',
          userId: ''
        },
        rules: {
          debtor: [
            {required: true, message: '请输入借款人姓名', trigger: 'change'}
          ],
          payer: [
            {required: true, message: '请输入还款人姓名', trigger: 'change'}
          ],
          userId: [
            {required: true, message: '请输入用户ID', trigger: 'change'}
          ],
          begin: [
            {type: 'date', required: true, message: '请选择日期', trigger: 'change'}
          ],
          money: [
            {required: true, pattern: /^[+-]?[0-9]+(\.[0-9]{1,2})?$/, message: '数据应为整数或小数，保留2位小数', trigger: 'blur'}
          ]

        }
      };
    },methods: {
      submitForm(formName) {
        let _this=this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.ruleForm);


            let para = {
              userId: this.ruleForm.userId,
              did: this.ruleForm.did,
              debtor: this.ruleForm.debtor,
              payer: this.ruleForm.payer,
              begin: this.ruleForm.begin,
              end: this.ruleForm.end,
              money: this.ruleForm.money,
            };
            console.log(para);
            _this.$axios.post("updDebt",para).then(resp => {
              if(resp.data === 'success'){
                _this.$alert('修改成功！', '消息', {
                  confirmButtonText: '确定',
                  callback: action => {
                    _this.$router.push('/adminDebt')
                  }
                })
              }
            })
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    created() {
      // let _this = this;
      // this.$axios.get("/user/"+this.$store.state.user.username).then(resp => {
      //   _this.ruleForm.userId = resp.data;
      // });

      let edata = this.$route.query.data;

      this.ruleForm.did = edata.did;
      this.ruleForm.debtor = edata.debtor;
      this.ruleForm.payer = edata.payer;
      this.ruleForm.money = edata.money;
      this.ruleForm.userId = edata.userId;
      this.ruleForm.begin = new Date(edata.begin);
      this.ruleForm.end = new Date(edata.end);
    }
  }
</script>

<style scoped>

</style>
